package com.work.keep.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.work.keep.R;

/**
 * Created by dllo on 16/1/21.
 */
public class UserInforActivityAdapter extends RecyclerView.Adapter {
    private static final int TYPE_HEADER = 0, TYPE_ITEM = 1, TYPE_FOOT = 2;
    private View headView;
    private Context context;
    private View footView;
    private int headViewSize = 0;
    private int footViewSize = 0;
    private boolean isAddFoot = false;
    private boolean isAddHead = false;
    private String inforLeft[] = {"昵称", "性别", "出生年月", "所在城市", "身高", "体重", "BMI", "简介"};
    private String inforRight[] = {"枫君", "男", "1993-01", "辽宁,大连市", "178cm", "75kg", "23.7", ""};

    public UserInforActivityAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case TYPE_HEADER:
                view = headView;
                break;

            case TYPE_ITEM:
                view = LayoutInflater.from(context).inflate(R.layout.item_activity_user, parent, false);
                return new MyViewHolder(view);

            case TYPE_FOOT:
                view = footView;
                break;
        }
        return new MyHeadHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_ITEM) {
            ((MyViewHolder) holder).nickname.setText(inforRight[position - headViewSize]);
            ((MyViewHolder) holder).nicknameText.setText(inforLeft[position - headViewSize]);
        }
    }

    @Override
    public int getItemViewType(int position) {
        int type = TYPE_ITEM;

        if (headViewSize == 1 && position == 0) {
            type = TYPE_HEADER;
        } else if (footViewSize == 1 && position == getItemCount() - 1) {
            //最后一个位置
            type = TYPE_FOOT;
        }
        return type;
    }

    public void addHeadView(View view) {
        headView = view;
        headViewSize = 1;
        isAddHead = true;
    }

    public void addFootView(View view) {
        footView = view;
        footViewSize = 1;
        isAddFoot = true;
    }

    @Override
    public int getItemCount() {
        return inforLeft.length + headViewSize + footViewSize;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView nicknameText, nickname;

        public MyViewHolder(View itemView) {
            super(itemView);
            nickname = (TextView) itemView.findViewById(R.id.nickname);
            nicknameText = (TextView) itemView.findViewById(R.id.nicknameText);
        }
    }

    class MyHeadHolder extends RecyclerView.ViewHolder {

        public MyHeadHolder(View itemView) {
            super(itemView);
        }
    }
}
